<?php
namespace app\mouth\model;
use think\Db,
    app\mouth\common\Encapsulation;
class CircleModel extends Encapsulation{
    public static function index(){
       $value= Db::table('yc_circle')
//           ->field('id,background,name,join,works')
           ->order('join desc')
           ->select();
       return $value;
    }
    public function joinTheCircle($cid,$uid,$date){
        $where = [
            'c_id'=>$cid,
            'u_id'=>$uid
        ];
        if ($date === 2){
            $this->setDec('circle',['id'=>$cid],'join');
            $value = $this->delete('circle_details',$where);
        }else{
            $oen = $this->one('circle_details',$where,'*');
            if (!empty($oen)){
                return false;
            }
            $this->setInc('circle',['id'=>$cid],'join');
            $value = $this->insert('circle_details',$where);
        }
       if ($value){
           return true;
       }
       return false;

    }
    public static function circleDetails($id,$uid){
        $add    = Db::name('circle_details')->where('c_id',$id)->where('u_id',$uid)->find();//是否加入
        $value['join'] = intval($add);
        $value['newest']    = Db::name('circle_invitation')->where('c_id',$id)->order('add_time desc')->select();//最新帖子
        $value['hottest']   = Db::name('circle_invitation')->where('c_id',$id)->order('browse desc')->select();//最热按照浏览量排序
        $value['user']      = Db::table('yc_circle_details')->alias('d')//所有用户
                            ->join('yc_circle_user u','d.u_id = u.id')
                            ->where('d.c_id',$id)
                            ->field('u.portrait,u.gender,u.nickname')
                            ->select();
        return $value;
    }
    public static function Search($name){
        $field = 'id,cover,name,describe,background';
        return Db::name('circle')->field($field)->where('name','like','%'.$name.'%')->select();
    }
    public static function Attention($id){
        Db::name('circle_details')->where('id',$id['c_id'])->setInc('join');
        return Db::name('circle_details')->insert($id);
    }
    public static function canceAttention($u_id,$c_id){
        Db::name('circle_details')->where('id',$c_id)->setDec('join');
        return Db::name('circle_details')->where('c_id',$c_id)->where('u_id',$u_id)->delete();
    }
    public function joinThePost($date,$Deposit){

        $add = $this->add($date,$Deposit);
        if (!is_array($add)){
            return false;
        }
        Db::name('circle')->where('id',$add['c_id'])->setInc('works');
        Db::name('circle_invitation')->insert($add);
        return true;
    }
    public function add($date,$Deposit){
        if (empty($date['content'])){
            return false;
        }
        if (!is_numeric($date['c_id'])){
            return false;
        }
        $value['content']   = $date['content'];
        $value['u_id']      = $Deposit['uid'];
        $value['c_id']      = $date['c_id'];
        $value['nickname']  = $Deposit['nickname'];
        $value['portrait']  = $Deposit['portrait'];
        $value['picture']  = $date['picture'] != '' ? $date['picture'] : '';
        $value['add_time']  = time();
        return $value;
    }
}